package site.immonkey.ftptest.beans;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FtpFile {
	private boolean directory;
	private String name;
	private long size;
	private boolean hide;

	public static FtpFile parser(String str) {
		Pattern pattern = Pattern.compile("\\s+");
		Matcher matcher = pattern.matcher(str);
		int firstIndex = 0;
		FtpFile file = new FtpFile();
		for (int i = 0; i < 8; i++) {
			if (!matcher.find()) throw new RuntimeException("FtpFile parser error : " + str);
			String text = str.substring(firstIndex, matcher.start());
			switch (i) {
				case 0:
					file.setDirectory(text.toLowerCase().startsWith("d"));
					break;
				case 4:
					file.setSize(Long.parseLong(text));
					break;
				case 7:
					String name = str.substring(matcher.end()).trim();
					file.setName(name);
					file.setHide(name.startsWith("."));
					break;
			}
			firstIndex = matcher.end();
		}
		return file;
	}

	public void setDirectory(boolean directory) {
		this.directory = directory;
	}

	public boolean isDirectory() {
		return directory;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public long getSize() {
		return size;
	}

	public void setSize(long size) {
		this.size = size;
	}

	public void setHide(boolean hide) {
		this.hide = hide;
	}

	public boolean isHide() {
		return hide;
	}

	@Override
	public String toString() {
		return "FtpFile [isDirectory=" + directory + ", name=" + name + ", size=" + size + ", isHide=" + hide + "]";
	}

}
